<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
         <!--
          <a-col :span="12">
            <a-form-item label="仓库编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['warehouseId', validatorRules.warehouseId]"
                placeholder="请输入仓库编号"
              ></a-input>
            </a-form-item>
          </a-col>
-->
          <a-col :span="12">
            <a-form-item label="仓库编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['warehouseCode', validatorRules.warehouseCode]"
                placeholder="请输入仓库编码"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['warehouseName', validatorRules.warehouseName]"
                placeholder="请输入仓库名称"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="所属BU" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                :disabled="disableSubmit"
                v-decorator="['ownerBuId']"
                dict="DataDictionary,description,dictionaryCode,dictionaryTypeId=BU_DEPARTMENT"
                placeholder="请选择BU"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="建设冷冻容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="disableSubmit"
                v-decorator="['buildingFreeze']"
                placeholder="请输入建设冷冻容量"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="建设冷藏容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="disableSubmit"
                v-decorator="['buildingRefrigerate']"
                placeholder="请输入建设冷藏容量"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="建设恒温容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="disableSubmit"
                v-decorator="['buildingConstant']"
                placeholder="请输入建设恒温容量"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="建设常温容量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="disableSubmit"
                v-decorator="['buildingNormal']"
                placeholder="请输入建设常温容量"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['remark']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
export default {
  name: 'CrmWmsInventoryModal',
  components: {
    JSearchSelectTag,
  },
  data() {
    return {
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 1000,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        warehouseId: {
          rules: [{ required: true, message: '请输入仓库编号!' }],
        },
        warehouseCode: {
          rules: [{ required: true, message: '请输入仓库编码!' }],
        },
        warehouseName: {
          rules: [{ required: true, message: '请输入仓库名称!' }],
        },
      },
      url: {
        add: '/online/api/createCrmWmsInventory',
        edit: '/online/api/updateCrmWmsInventory',
        get: '/online/api/getCrmWmsInventory',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.id) {
        httpAction(this.url.get, { id: record.id }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'warehouseId',
                  'warehouseCode',
                  'warehouseName',
                  'buildingFreeze',
                  'buildingRefrigerate',
                  'buildingConstant',
                  'buildingNormal',
                  'physicalFreeze',
                  'physicalRefrigerate',
                  'physicalConstant',
                  'physicalNormal',
                  'remark',
                  'createUserId',
                  'ownerUserId',
                  'ownerBuId',
                  'createTime',
                  'updateTime'
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'warehouseId',
          'warehouseCode',
          'warehouseName',
          'buildingFreeze',
          'buildingRefrigerate',
          'buildingConstant',
          'buildingNormal',
          'physicalFreeze',
          'physicalRefrigerate',
          'physicalConstant',
          'physicalNormal',
          'remark',
          'createUserId',
          'ownerUserId',
          'ownerBuId',
          'createTime',
          'updateTime'
        )
      )
    },
  },
}
</script>
